home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-4 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  48KB  |  956 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Predicates on Numbers,  Next: Comparison of Numbers,  Prev: Float Basics,  Up: Numbers
  20. Type Predicates for Numbers
  21. ===========================
  22.    The functions in this section test whether the argument is a number
  23. or whether it is a certain sort of number.  The functions `integerp'
  24. and `floatp' can take any type of Lisp object as argument (the
  25. predicates would not be of much use otherwise); but the `zerop'
  26. predicate requires a number as its argument.  See also
  27. `integer-or-marker-p' and `number-or-marker-p', in *Note Predicates on
  28. Markers::.
  29.  - Function: floatp OBJECT
  30.      This predicate tests whether its argument is a floating point
  31.      number and returns `t' if so, `nil' otherwise.
  32.      `floatp' does not exist in Emacs versions 18 and earlier.
  33.  - Function: integerp OBJECT
  34.      This predicate tests whether its argument is an integer, and
  35.      returns `t' if so, `nil' otherwise.
  36.  - Function: numberp OBJECT
  37.      This predicate tests whether its argument is a number (either
  38.      integer or floating point), and returns `t' if so, `nil' otherwise.
  39.  - Function: natnump OBJECT
  40.      The `natnump' predicate (whose name comes from the phrase
  41.      "natural-number-p") tests to see whether its argument is a
  42.      nonnegative integer, and returns `t' if so, `nil' otherwise.  0 is
  43.      considered non-negative.
  44.      Markers are not converted to integers, hence `natnump' of a marker
  45.      is always `nil'.
  46.      People have pointed out that this function is misnamed, because
  47.      the term "natural number" is usually understood as excluding zero.
  48.      We are open to suggestions for a better name to use in a future
  49.      version.
  50.  - Function: zerop NUMBER
  51.      This predicate tests whether its argument is zero, and returns `t'
  52.      if so, `nil' otherwise.  The argument must be a number.
  53.      These two forms are equivalent: `(zerop x) == (= x 0)'.
  54. File: elisp,  Node: Comparison of Numbers,  Next: Numeric Conversions,  Prev: Predicates on Numbers,  Up: Numbers
  55. Comparison of Numbers
  56. =====================
  57.    Floating point numbers in Emacs Lisp actually take up storage, and
  58. there can be many distinct floating point number objects with the same
  59. numeric value.  If you use `eq' to compare them, then you test whether
  60. two values are the same *object*.  If you want to compare just the
  61. numeric values, use `='.
  62.    If you use `eq' to compare two integers, it always returns `t' if
  63. they have the same value.  This is sometimes useful, because `eq'
  64. accepts arguments of any type and never causes an error, whereas `='
  65. signals an error if the arguments are not numbers or markers.  However,
  66. it is a good idea to use `=' if you can, even for comparing integers,
  67. just in case we change the representation of integers in a future Emacs
  68. version.
  69.    There is another wrinkle: because floating point arithmetic is not
  70. exact, it is often a bad idea to check for equality of two floating
  71. point values.  Usually it is better to test for approximate equality.
  72. Here's a function to do this:
  73.      (defvar fuzz-factor 1.0e-6)
  74.      
  75.      (defun approx-equal (x y)
  76.        (< (/ (abs (- x y))
  77.              (max (abs x) (abs y)))
  78.           fuzz-factor))
  79.      Common Lisp note: because of the way numbers are implemented in
  80.      Common Lisp, you generally need to use ``='' to test for equality
  81.      between numbers of any kind.
  82.  - Function: = NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  83.      This function tests whether its arguments are the same number, and
  84.      returns `t' if so, `nil' otherwise.
  85.  - Function: /= NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  86.      This function tests whether its arguments are not the same number,
  87.      and returns `t' if so, `nil' otherwise.
  88.  - Function: < NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  89.      This function tests whether its first argument is strictly less
  90.      than its second argument.  It returns `t' if so, `nil' otherwise.
  91.  - Function: <= NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  92.      This function tests whether its first argument is less than or
  93.      equal to its second argument.  It returns `t' if so, `nil'
  94.      otherwise.
  95.  - Function: > NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  96.      This function tests whether its first argument is strictly greater
  97.      than its second argument.  It returns `t' if so, `nil' otherwise.
  98.  - Function: >= NUMBER-OR-MARKER1 NUMBER-OR-MARKER2
  99.      This function tests whether its first argument is greater than or
  100.      equal to its second argument.  It returns `t' if so, `nil'
  101.      otherwise.
  102.  - Function: max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS
  103.      This function returns the largest of its arguments.
  104.           (max 20)
  105.                => 20
  106.           (max 1 2)
  107.                => 2
  108.           (max 1 3 2)
  109.                => 3
  110.  - Function: min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS
  111.      This function returns the smallest of its arguments.
  112. File: elisp,  Node: Numeric Conversions,  Next: Arithmetic Operations,  Prev: Comparison of Numbers,  Up: Numbers
  113. Numeric Conversions
  114. ===================
  115.    To convert an integer to floating point, use the function `float'.
  116.  - Function: float NUMBER
  117.      This returns NUMBER converted to floating point.  If NUMBER is
  118.      already a floating point number, `float' returns it unchanged.
  119.    There are four functions to convert floating point numbers to
  120. integers; they differ in how they round.  You can call these functions
  121. with an integer argument also; if you do, they return it without change.
  122.  - Function: truncate NUMBER
  123.      This returns NUMBER, converted to an integer by rounding towards
  124.      zero.
  125.  - Function: floor NUMBER
  126.      This returns NUMBER, converted to an integer by rounding downward
  127.      (towards negative infinity).
  128.  - Function: ceiling NUMBER
  129.      This returns NUMBER, converted to an integer by rounding upward
  130.      (towards positive infinity).
  131.  - Function: round NUMBER
  132.      This returns NUMBER, converted to an integer by rounding towards
  133.      the nearest integer.
  134. File: elisp,  Node: Arithmetic Operations,  Next: Bitwise Operations,  Prev: Numeric Conversions,  Up: Numbers
  135. Arithmetic Operations
  136. =====================
  137.    Emacs Lisp provides the traditional four arithmetic operations:
  138. addition, subtraction, multiplication, and division.  A remainder
  139. function supplements the (integer) division function.  The functions to
  140. add or subtract 1 are provided because they are traditional in Lisp and
  141. commonly used.
  142.    All of these functions except `%' return a floating point value if
  143. any argument is floating.
  144.    It is important to note that in GNU Emacs Lisp, arithmetic functions
  145. do not check for overflow.  Thus `(1+ 8388607)' may equal -8388608,
  146. depending on your hardware.
  147.  - Function: 1+ NUMBER-OR-MARKER
  148.      This function returns NUMBER-OR-MARKER plus 1.  For example,
  149.           (setq foo 4)
  150.                => 4
  151.           (1+ foo)
  152.                => 5
  153.      This function is not analogous to the C operator `++'--it does not
  154.      increment a variable.  It just computes a sum.  Thus,
  155.           foo
  156.                => 4
  157.      If you want to increment the variable, you must use `setq', like
  158.      this:
  159.           (setq foo (1+ foo))
  160.                => 5
  161.  - Function: 1- NUMBER-OR-MARKER
  162.      This function returns NUMBER-OR-MARKER minus 1.
  163.  - Function: abs NUMBER
  164.      This returns the absolute value of NUMBER.
  165.  - Function: + &rest NUMBERS-OR-MARKERS
  166.      This function adds its arguments together.  When given no
  167.      arguments, `+' returns 0.  It does not check for overflow.
  168.           (+)
  169.                => 0
  170.           (+ 1)
  171.                => 1
  172.           (+ 1 2 3 4)
  173.                => 10
  174.  - Function: - &optional NUMBER-OR-MARKER &rest OTHER-NUMBERS-OR-MARKERS
  175.      The `-' function serves two purposes: negation and subtraction.
  176.      When `-' has a single argument, the value is the negative of the
  177.      argument.  When there are multiple arguments, each of the
  178.      OTHER-NUMBERS-OR-MARKERS is subtracted from NUMBER-OR-MARKER,
  179.      cumulatively.  If there are no arguments, the result is 0.  This
  180.      function does not check for overflow.
  181.           (- 10 1 2 3 4)
  182.                => 0
  183.           (- 10)
  184.                => -10
  185.           (-)
  186.                => 0
  187.  - Function: * &rest NUMBERS-OR-MARKERS
  188.      This function multiplies its arguments together, and returns the
  189.      product.  When given no arguments, `*' returns 1.  It does not
  190.      check for overflow.
  191.           (*)
  192.                => 1
  193.           (* 1)
  194.                => 1
  195.           (* 1 2 3 4)
  196.                => 24
  197.  - Function: / DIVIDEND DIVISOR &rest DIVISORS
  198.      This function divides DIVIDEND by DIVISORS and returns the
  199.      quotient.  If there are additional arguments DIVISORS, then
  200.      DIVIDEND is divided by each divisor in turn.  Each argument may be
  201.      a number or a marker.
  202.      If all the arguments are integers, then the result is an integer
  203.      too.  This means the result has to be rounded.  On most machines,
  204.      the result is rounded towards zero after each division, but some
  205.      machines may round differently with negative arguments.  This is
  206.      because the Lisp function `/' is implemented using the C division
  207.      operator, which has the same possibility for machine-dependent
  208.      rounding.  As a practical matter, all known machines round in the
  209.      standard fashion.
  210.      If you divide by 0, an `arith-error' error is signaled.  (*Note
  211.      Errors::.)
  212.           (/ 6 2)
  213.                => 3
  214.           (/ 5 2)
  215.                => 2
  216.           (/ 25 3 2)
  217.                => 4
  218.           (/ -17 6)
  219.                => -2
  220.      Since the division operator in Emacs Lisp is implemented using the
  221.      division operator in C, the result of dividing negative numbers
  222.      may in principle vary from machine to machine, depending on how
  223.      they round the result.  Thus, the result of `(/ -17 6)' could be
  224.      -3 on some machines.  In practice, nearly all machines round the
  225.      quotient towards 0.
  226.  - Function: % DIVIDEND DIVISOR
  227.      This function returns the value of DIVIDEND modulo DIVISOR; in
  228.      other words, the integer remainder after division of DIVIDEND by
  229.      DIVISOR.  The sign of the result is the sign of DIVIDEND.  The
  230.      sign of DIVISOR is ignored.  The arguments must be integers.
  231.      For negative arguments, the value is in principle machine-dependent
  232.      since the quotient is; but in practice, all known machines behave
  233.      alike.
  234.      An `arith-error' results if DIVISOR is 0.
  235.           (% 9 4)
  236.                => 1
  237.           (% -9 4)
  238.                => -1
  239.           (% 9 -4)
  240.                => 1
  241.           (% -9 -4)
  242.                => -1
  243.      For any two numbers DIVIDEND and DIVISOR,
  244.           (+ (% DIVIDEND DIVISOR)
  245.              (* (/ DIVIDEND DIVISOR) DIVISOR))
  246.      always equals DIVIDEND.
  247. File: elisp,  Node: Bitwise Operations,  Next: Transcendental Functions,  Prev: Arithmetic Operations,  Up: Numbers
  248. Bitwise Operations on Integers
  249. ==============================
  250.    In a computer, an integer is represented as a binary number, a
  251. sequence of "bits" (digits which are either zero or one).  A bitwise
  252. operation acts on the individual bits of such a sequence.  For example,
  253. "shifting" moves the whole sequence left or right one or more places,
  254. reproducing the same pattern "moved over".
  255.    The bitwise operations in Emacs Lisp apply only to integers.
  256.  - Function: lsh INTEGER1 COUNT
  257.      `lsh', which is an abbreviation for "logical shift", shifts the
  258.      bits in INTEGER1 to the left COUNT places, or to the right if
  259.      COUNT is negative.  If COUNT is negative, `lsh' shifts zeros into
  260.      the most-significant bit, producing a positive result even if
  261.      INTEGER1 is negative.  Contrast this with `ash', below.
  262.      Thus, the decimal number 5 is the binary number 00000101.  Shifted
  263.      once to the left, with a zero put in the one's place, the number
  264.      becomes 00001010, decimal 10.
  265.      Here are two examples of shifting the pattern of bits one place to
  266.      the left.  Since the contents of the rightmost place has been
  267.      moved one place to the left, a value has to be inserted into the
  268.      rightmost place.  With `lsh', a zero is placed into the rightmost
  269.      place.  (These examples show only the low-order eight bits of the
  270.      binary pattern; the rest are all zero.)
  271.           (lsh 5 1)
  272.                => 10
  273.           
  274.           ;; Decimal 5 becomes decimal 10.
  275.           00000101 => 00001010
  276.           
  277.           (lsh 7 1)
  278.                => 14
  279.           
  280.           ;; Decimal 7 becomes decimal 14.
  281.           00000111 => 00001110
  282.      As the examples illustrate, shifting the pattern of bits one place
  283.      to the left produces a number that is twice the value of the
  284.      previous number.
  285.      Note, however that functions do not check for overflow, and a
  286.      returned value may be negative (and in any case, no more than a 24
  287.      bit value) when an integer is sufficiently left shifted.
  288.      For example, left shifting 8,388,607 produces -2:
  289.           (lsh 8388607 1)          ; left shift
  290.                => -2
  291.      In binary, in the 24 bit implementation, the numbers looks like
  292.      this:
  293.           ;; Decimal 8,388,607
  294.           0111 1111  1111 1111  1111 1111
  295.      which becomes the following when left shifted:
  296.           ;; Decimal -2
  297.           1111 1111  1111 1111  1111 1110
  298.      Shifting the pattern of bits two places to the left produces
  299.      results like this (with 8-bit binary numbers):
  300.           (lsh 3 2)
  301.                => 12
  302.           
  303.           ;; Decimal 3 becomes decimal 12.
  304.           00000011 => 00001100
  305.      On the other hand, shifting the pattern of bits one place to the
  306.      right looks like this:
  307.           (lsh 6 -1)
  308.                => 3
  309.           
  310.           ;; Decimal 6 becomes decimal 3.
  311.           00000110 => 00000011
  312.           
  313.           (lsh 5 -1)
  314.                => 2
  315.           
  316.           ;; Decimal 5 becomes decimal 2.
  317.           00000101 => 00000010
  318.      As the example illustrates, shifting the pattern of bits one place
  319.      to the right divides the value of the binary number by two,
  320.      rounding downward.
  321.  - Function: ash INTEGER1 COUNT
  322.      `ash' ("arithmetic shift") shifts the bits in INTEGER1 to the left
  323.      COUNT places, or to the right if COUNT is negative.
  324.      `ash' gives the same results as `lsh' except when INTEGER1 and
  325.      COUNT are both negative.  In that case, `ash' puts a one in the
  326.      leftmost position, while `lsh' puts a zero in the leftmost
  327.      position.
  328.      Thus, with `ash', shifting the pattern of bits one place to the
  329.      right looks like this:
  330.           (ash -6 -1)
  331.                => -3
  332.           
  333.           ;; Decimal -6
  334.           ;; becomes decimal -3.
  335.           
  336.           1111 1111  1111 1111  1111 1010
  337.                =>
  338.           1111 1111  1111 1111  1111 1101
  339.      In contrast, shifting the pattern of bits one place to the right
  340.      with `lsh' looks like this:
  341.           (lsh -6 -1)
  342.                => 8388605
  343.           
  344.           ;; Decimal -6
  345.           ;; becomes decimal 8,388,605.
  346.           
  347.           1111 1111  1111 1111  1111 1010
  348.                =>
  349.           0111 1111  1111 1111  1111 1101
  350.      In this case, the 1 in the leftmost position is shifted one place
  351.      to the right, and a zero is shifted into the leftmost position.
  352.      Here are other examples:
  353.           ;               24-bit binary values
  354.           
  355.           (lsh 5 2)          ;   5  =  0000 0000  0000 0000  0000 0101
  356.                => 20         ;  20  =  0000 0000  0000 0000  0001 0100
  357.           (ash 5 2)
  358.                => 20
  359.           (lsh -5 2)         ;  -5  =  1111 1111  1111 1111  1111 1011
  360.                => -20        ; -20  =  1111 1111  1111 1111  1110 1100
  361.           (ash -5 2)
  362.                => -20
  363.           (lsh 5 -2)         ;   5  =  0000 0000  0000 0000  0000 0101
  364.                => 1          ;   1  =  0000 0000  0000 0000  0000 0001
  365.           (ash 5 -2)
  366.                => 1
  367.           (lsh -5 -2)        ;  -5  =  1111 1111  1111 1111  1111 1011
  368.                => 4194302    ;         0011 1111  1111 1111  1111 1110
  369.           (ash -5 -2)        ;  -5  =  1111 1111  1111 1111  1111 1011
  370.                => -2         ;  -2  =  1111 1111  1111 1111  1111 1110
  371.  - Function: logand &rest INTS-OR-MARKERS
  372.      This function returns the "logical and" of the arguments: the Nth
  373.      bit is set in the result if, and only if, the Nth bit is set in
  374.      all the arguments.  ("Set" means that the value of the bit is 1
  375.      rather than 0.)
  376.      For example, using 4-bit binary numbers, the "logical and" of 13
  377.      and 12 is 12: 1101 combined with 1100 produces 1100.
  378.      In both the binary numbers, the leftmost two bits are set (i.e.,
  379.      they are 1's), so the leftmost two bits of the returned value are
  380.      set.  However, for the rightmost two bits, each is zero in at
  381.      least one of the arguments, so the rightmost two bits of the
  382.      returned value are 0's.
  383.      Therefore,
  384.           (logand 13 12)
  385.                => 12
  386.      If `logand' is not passed any argument, it returns a value of -1.
  387.      This number is an identity element for `logand' because its binary
  388.      representation consists entirely of ones.  If `logand' is passed
  389.      just one argument, it returns that argument.
  390.           ;                24-bit binary values
  391.           
  392.           (logand 14 13)     ; 14  =  0000 0000  0000 0000  0000 1110
  393.                              ; 13  =  0000 0000  0000 0000  0000 1101
  394.                => 12         ; 12  =  0000 0000  0000 0000  0000 1100
  395.           (logand 14 13 4)   ; 14  =  0000 0000  0000 0000  0000 1110
  396.                              ; 13  =  0000 0000  0000 0000  0000 1101
  397.                              ;  4  =  0000 0000  0000 0000  0000 0100
  398.                => 4          ;  4  =  0000 0000  0000 0000  0000 0100
  399.           (logand)
  400.                => -1         ; -1  =  1111 1111  1111 1111  1111 1111
  401.  - Function: logior &rest INTS-OR-MARKERS
  402.      This function returns the "inclusive or" of its arguments: the Nth
  403.      bit is set in the result if, and only if, the Nth bit is set in at
  404.      least one of the arguments.  If there are no arguments, the result
  405.      is zero, which is an identity element for this operation.  If
  406.      `logior' is passed just one argument, it returns that argument.
  407.           ;               24-bit binary values
  408.           
  409.           (logior 12 5)      ; 12  =  0000 0000  0000 0000  0000 1100
  410.                              ;  5  =  0000 0000  0000 0000  0000 0101
  411.                => 13         ; 13  =  0000 0000  0000 0000  0000 1101
  412.           (logior 12 5 7)    ; 12  =  0000 0000  0000 0000  0000 1100
  413.                              ;  5  =  0000 0000  0000 0000  0000 0101
  414.                              ;  7  =  0000 0000  0000 0000  0000 0111
  415.                => 15         ; 15  =  0000 0000  0000 0000  0000 1111
  416.  - Function: logxor &rest INTS-OR-MARKERS
  417.      This function returns the "exclusive or" of its arguments: the Nth
  418.      bit is set in the result if, and only if, the Nth bit is set in an
  419.      odd number of the arguments.  If there are no arguments, the
  420.      result is 0.  If `logxor' is passed just one argument, it returns
  421.      that argument.
  422.           ;               24-bit binary values
  423.           
  424.           (logxor 12 5)      ; 12  =  0000 0000  0000 0000  0000 1100
  425.                              ;  5  =  0000 0000  0000 0000  0000 0101
  426.                => 9          ;  9  =  0000 0000  0000 0000  0000 1001
  427.           (logxor 12 5 7)    ; 12  =  0000 0000  0000 0000  0000 1100
  428.                              ;  5  =  0000 0000  0000 0000  0000 0101
  429.                              ;  7  =  0000 0000  0000 0000  0000 0111
  430.                => 14         ; 14  =  0000 0000  0000 0000  0000 1110
  431.  - Function: lognot INTEGER
  432.      This function returns the logical complement of its argument: the
  433.      Nth bit is one in the result if, and only if, the Nth bit is zero
  434.      in INTEGER, and vice-versa.
  435.           ;;  5  =  0000 0000  0000 0000  0000 0101
  436.           ;; becomes
  437.           ;; -6  =  1111 1111  1111 1111  1111 1010
  438.           
  439.           (lognot 5)
  440.                => -6
  441. File: elisp,  Node: Transcendental Functions,  Next: Random Numbers,  Prev: Bitwise Operations,  Up: Numbers
  442. Transcendental Functions
  443. ========================
  444.    These mathematical functions are available if floating point is
  445. supported.  They allow integers as well as floating point numbers as
  446. arguments.
  447.  - Function: sin ARG
  448.  - Function: cos ARG
  449.  - Function: tan ARG
  450.      These are the ordinary trigonometric functions, with argument
  451.      measured in radians.
  452.  - Function: asin ARG
  453.      The value of `(asin ARG)' is a number between - pi / 2 and pi / 2
  454.      (inclusive) whose sine is ARG; if, however, ARG is out of range
  455.      (outside [-1, 1]), then the result is a NaN.
  456.  - Function: acos ARG
  457.      The value of `(acos ARG)' is a number between 0 and pi (inclusive)
  458.      whose cosine is ARG; if, however, ARG is out of range (outside
  459.      [-1, 1]), then the result is a NaN.
  460.  - Function: atan ARG
  461.      The value of `(atan ARG)' is a number between - pi / 2 and pi / 2
  462.      (exclusive) whose tangent is ARG.
  463.  - Function: exp ARG
  464.      This is the exponential function; it returns e to the power ARG.
  465.  - Function: log ARG &optional BASE
  466.      This function returns the logarithm of ARG, with base BASE.  If
  467.      you don't specify BASE, the base E is used.  If ARG is negative,
  468.      the result is a NaN.
  469.  - Function: log10 ARG
  470.      This function returns the logarithm of ARG, with base 10.  If ARG
  471.      is negative, the result is a NaN.
  472.  - Function: expt X Y
  473.      This function returns X raised to power Y.
  474.  - Function: sqrt ARG
  475.      This returns the square root of ARG.
  476. File: elisp,  Node: Random Numbers,  Prev: Transcendental Functions,  Up: Numbers
  477. Random Numbers
  478. ==============
  479.    In a computer, a series of pseudo-random numbers is generated in a
  480. deterministic fashion.  The numbers are not truly random, but they have
  481. certain properties that mimic a random series.  For example, all
  482. possible values occur equally often in a pseudo-random series.
  483.    In Emacs, pseudo-random numbers are generated from a "seed" number.
  484. Starting from any given seed, the `random' function always generates
  485. the same sequence of numbers.  Emacs always starts with the same seed
  486. value, so the sequence of values of `random' is actually the same in
  487. each Emacs run!  For example, in one operating system, the first call
  488. to `(random)' after you start Emacs always returns -1457731, and the
  489. second one always returns -7692030.  This is helpful for debugging.
  490.    If you want truly unpredictable random numbers, execute `(random
  491. t)'.  This chooses a new seed based on the current time of day and on
  492. Emacs' process ID number.
  493.  - Function: random &optional LIMIT
  494.      This function returns a pseudo-random integer.  When called more
  495.      than once, it returns a series of pseudo-random integers.
  496.      If LIMIT is `nil', then the value may in principle be any integer.
  497.      If LIMIT is a positive integer, the value is chosen to be
  498.      nonnegative and less than LIMIT (only in Emacs 19).
  499.      If LIMIT is `t', it means to choose a new seed based on the
  500.      current time of day and on Emacs's process ID number.
  501.      On some machines, any integer representable in Lisp may be the
  502.      result of `random'.  On other machines, the result can never be
  503.      larger than a certain maximum or less than a certain (negative)
  504.      minimum.
  505. File: elisp,  Node: Strings and Characters,  Next: Lists,  Prev: Numbers,  Up: Top
  506. Strings and Characters
  507. **********************
  508.    A string in Emacs Lisp is an array that contains an ordered sequence
  509. of characters.  Strings are used as names of symbols, buffers, and
  510. files, to send messages to users, to hold text being copied between
  511. buffers, and for many other purposes.  Because strings are so
  512. important, many functions are provided expressly for manipulating them.
  513. Emacs Lisp programs use strings more often than individual characters.
  514. * Menu:
  515. * Intro to Strings::          Basic properties of strings and characters.
  516. * Predicates for Strings::    Testing whether an object is a string or char.
  517. * Creating Strings::          Functions to allocate new strings.
  518. * Text Comparison::           Comparing characters or strings.
  519. * String Conversion::         Converting characters or strings and vice versa.
  520. * Formatting Strings::        `format': Emacs's analog of `printf'.
  521. * Character Case::            Case conversion functions.
  522. * Case Table::              Customizing case conversion.
  523.    *Note Strings of Events::, for special considerations when using
  524. strings of keyboard character events.
  525. File: elisp,  Node: Intro to Strings,  Next: Predicates for Strings,  Up: Strings and Characters
  526. Introduction to Strings and Characters
  527. ======================================
  528.    Strings in Emacs Lisp are arrays that contain an ordered sequence of
  529. characters.  Characters are represented in Emacs Lisp as integers;
  530. whether an integer was intended as a character or not is determined only
  531. by how it is used.  Thus, strings really contain integers.
  532.    The length of a string (like any array) is fixed and independent of
  533. the string contents, and cannot be altered.  Strings in Lisp are *not*
  534. terminated by a distinguished character code.  (By contrast, strings in
  535. C are terminated by a character with ASCII code 0.) This means that any
  536. character, including the null character (ASCII code 0), is a valid
  537. element of a string.
  538.    Since strings are considered arrays, you can operate on them with the
  539. general array functions.  (*Note Sequences Arrays Vectors::.)  For
  540. example, you can access or change individual characters in a string
  541. using the functions `aref' and `aset' (*note Array Functions::.).
  542.    Each character in a string is stored in a single byte.  Therefore,
  543. numbers not in the range 0 to 255 are truncated when stored into a
  544. string.  This means that a string takes up much less memory than a
  545. vector of the same length.
  546.    Sometimes key sequences are represented as strings.  When a string is
  547. a key sequence, string elements in the range 128 to 255 represent meta
  548. characters (which are extremely large integers) rather than keyboard
  549. events in the range 128 to 255.
  550.    Strings cannot hold characters that have the hyper, super or alt
  551. modifiers; they can hold ASCII control characters, but no others.  They
  552. do not distinguish case in ASCII control characters.  *Note Character
  553. Type::, for more information about representation of meta and other
  554. modifiers for keyboard input characters.
  555.    Like a buffer, a string can contain text properties for the
  556. characters in it, as well as the characters themselves.  *Note Text
  557. Properties::.
  558.    *Note Text::, for information about functions that display strings or
  559. copy them into buffers.  *Note Character Type::, and *Note String
  560. Type::, for information about the syntax of characters and strings.
  561. File: elisp,  Node: Predicates for Strings,  Next: Creating Strings,  Prev: Intro to Strings,  Up: Strings and Characters
  562. The Predicates for Strings
  563. ==========================
  564.    For more information about general sequence and array predicates,
  565. see *Note Sequences Arrays Vectors::, and *Note Arrays::.
  566.  - Function: stringp OBJECT
  567.      This function returns `t' if OBJECT is a string, `nil' otherwise.
  568.  - Function: char-or-string-p OBJECT
  569.      This function returns `t' if OBJECT is a string or a character
  570.      (i.e., an integer), `nil' otherwise.
  571. File: elisp,  Node: Creating Strings,  Next: Text Comparison,  Prev: Predicates for Strings,  Up: Strings and Characters
  572. Creating Strings
  573. ================
  574.    The following functions create strings, either from scratch, or by
  575. putting strings together, or by taking them apart.
  576.  - Function: make-string COUNT CHARACTER
  577.      This function returns a string made up of COUNT repetitions of
  578.      CHARACTER.  If COUNT is negative, an error is signaled.
  579.           (make-string 5 ?x)
  580.                => "xxxxx"
  581.           (make-string 0 ?x)
  582.                => ""
  583.      Other functions to compare with this one include `char-to-string'
  584.      (*note String Conversion::.), `make-vector' (*note Vectors::.), and
  585.      `make-list' (*note Building Lists::.).
  586.  - Function: substring STRING START &optional END
  587.      This function returns a new string which consists of those
  588.      characters from STRING in the range from (and including) the
  589.      character at the index START up to (but excluding) the character
  590.      at the index END.  The first character is at index zero.
  591.           (substring "abcdefg" 0 3)
  592.                => "abc"
  593.      Here the index for `a' is 0, the index for `b' is 1, and the index
  594.      for `c' is 2.  Thus, three letters, `abc', are copied from the
  595.      full string.  The index 3 marks the character position up to which
  596.      the substring is copied.  The character whose index is 3 is
  597.      actually the fourth character in the string.
  598.      A negative number counts from the end of the string, so that -1
  599.      signifies the index of the last character of the string.  For
  600.      example:
  601.           (substring "abcdefg" -3 -1)
  602.                => "ef"
  603.      In this example, the index for `e' is -3, the index for `f' is -2,
  604.      and the index for `g' is -1.  Therefore, `e' and `f' are included,
  605.      and `g' is excluded.
  606.      When `nil' is used as an index, it falls after the last character
  607.      in the string.  Thus:
  608.           (substring "abcdefg" -3 nil)
  609.                => "efg"
  610.      Omitting the argument END is equivalent to specifying `nil'.  It
  611.      follows that `(substring STRING 0)' returns a copy of all of
  612.      STRING.
  613.           (substring "abcdefg" 0)
  614.                => "abcdefg"
  615.      But we recommend `copy-sequence' for this purpose (*note Sequence
  616.      Functions::.).
  617.      A `wrong-type-argument' error is signaled if either START or END
  618.      are non-integers.  An `args-out-of-range' error is signaled if
  619.      START indicates a character following END, or if either integer is
  620.      out of range for STRING.
  621.      Contrast this function with `buffer-substring' (*note Buffer
  622.      Contents::.), which returns a string containing a portion of the
  623.      text in the current buffer.  The beginning of a string is at index
  624.      0, but the beginning of a buffer is at index 1.
  625.  - Function: concat &rest SEQUENCES
  626.      This function returns a new string consisting of the characters in
  627.      the arguments passed to it.  The arguments may be strings, lists
  628.      of numbers, or vectors of numbers; they are not themselves
  629.      changed.  If no arguments are passed to `concat', it returns an
  630.      empty string.
  631.           (concat "abc" "-def")
  632.                => "abc-def"
  633.           (concat "abc" (list 120 (+ 256 121)) [122])
  634.                => "abcxyz"
  635.           (concat "The " "quick brown " "fox.")
  636.                => "The quick brown fox."
  637.           (concat)
  638.                => ""
  639.      The second example above shows how characters stored in strings are
  640.      taken modulo 256.  In other words, each character in the string is
  641.      stored in one byte.
  642.      The `concat' function always constructs a new string that is not
  643.      `eq' to any existing string.
  644.      When an argument is an integer (not a sequence of integers), it is
  645.      converted to a string of digits making up the decimal printed
  646.      representation of the integer.  This special case exists for
  647.      compatibility with Mocklisp, and we don't recommend you take
  648.      advantage of it.  If you want to convert an integer in this way,
  649.      use `format' (*note Formatting Strings::.) or `int-to-string'
  650.      (*note String Conversion::.).
  651.           (concat 137)
  652.                => "137"
  653.           (concat 54 321)
  654.                => "54321"
  655.      For information about other concatenation functions, see the
  656.      description of `mapconcat' in *Note Mapping Functions::, `vconcat'
  657.      in *Note Vectors::, and `append' in *Note Building Lists::.
  658. File: elisp,  Node: Text Comparison,  Next: String Conversion,  Prev: Creating Strings,  Up: Strings and Characters
  659. Comparison of Characters and Strings
  660. ====================================
  661.  - Function: char-equal CHARACTER1 CHARACTER2
  662.      This function returns `t' if the arguments represent the same
  663.      character, `nil' otherwise.  This function ignores differences in
  664.      case if `case-fold-search' is non-`nil'.
  665.           (char-equal ?x ?x)
  666.                => t
  667.           (char-to-string (+ 256 ?x))
  668.                => "x"
  669.           (char-equal ?x  (+ 256 ?x))
  670.                => t
  671.  - Function: string= STRING1 STRING2
  672.      This function returns `t' if the characters of the two strings
  673.      match exactly; case is significant.
  674.           (string= "abc" "abc")
  675.                => t
  676.           (string= "abc" "ABC")
  677.                => nil
  678.           (string= "ab" "ABC")
  679.                => nil
  680.  - Function: string-equal STRING1 STRING2
  681.      `string-equal' is another name for `string='.
  682.  - Function: string< STRING1 STRING2
  683.      This function compares two strings a character at a time.  First it
  684.      scans both the strings at once to find the first pair of
  685.      corresponding characters that do not match.  If the lesser
  686.      character of those two is the character from STRING1, then STRING1
  687.      is less, and this function returns `t'.  If the lesser character
  688.      is the one from STRING2, then STRING1 is greater, and this
  689.      function returns `nil'.  If the two strings match entirely, the
  690.      value is `nil'.
  691.      Pairs of characters are compared by their ASCII codes.  Keep in
  692.      mind that lower case letters have higher numeric values in the
  693.      ASCII character set than their upper case counterparts; numbers and
  694.      many punctuation characters have a lower numeric value than upper
  695.      case letters.
  696.           (string< "abc" "abd")
  697.                => t
  698.           (string< "abd" "abc")
  699.                => nil
  700.           (string< "123" "abc")
  701.                => t
  702.      When the strings have different lengths, and they match up to the
  703.      length of STRING1, then the result is `t'.  If they match up to
  704.      the length of STRING2, the result is `nil'.  A string without any
  705.      characters in it is the smallest possible string.
  706.           (string< "" "abc")
  707.                => t
  708.           (string< "ab" "abc")
  709.                => t
  710.           (string< "abc" "")
  711.                => nil
  712.           (string< "abc" "ab")
  713.                => nil
  714.           (string< "" "")
  715.                => nil
  716.  - Function: string-lessp STRING1 STRING2
  717.      `string-lessp' is another name for `string<'.
  718.    See `compare-buffer-substrings' in *Note Comparing Text::, for a way
  719. to compare text in buffers.
  720. File: elisp,  Node: String Conversion,  Next: Formatting Strings,  Prev: Text Comparison,  Up: Strings and Characters
  721. Conversion of Characters and Strings
  722. ====================================
  723.    Characters and strings may be converted into each other and into
  724. integers.  `format' and `prin1-to-string' (*note Output Functions::.)
  725. may also be used to convert Lisp objects into strings.
  726. `read-from-string' (*note Input Functions::.) may be used to "convert"
  727. a string representation of a Lisp object into an object.
  728.    *Note Documentation::, for a description of functions which return a
  729. string representing the Emacs standard notation of the argument
  730. character (`single-key-description' and `text-char-description').
  731. These functions are used primarily for printing help messages.
  732.  - Function: char-to-string CHARACTER
  733.      This function returns a new string with a length of one character.
  734.      The value of CHARACTER, modulo 256, is used to initialize the
  735.      element of the string.
  736.      This function is similar to `make-string' with an integer argument
  737.      of 1.  (*Note Creating Strings::.)  This conversion can also be
  738.      done with `format' using the `%c' format specification.  (*Note
  739.      Formatting Strings::.)
  740.           (char-to-string ?x)
  741.                => "x"
  742.           (char-to-string (+ 256 ?x))
  743.                => "x"
  744.           (make-string 1 ?x)
  745.                => "x"
  746.  - Function: string-to-char STRING
  747.      This function returns the first character in STRING.  If the
  748.      string is empty, the function returns 0.  The value is also 0 when
  749.      the first character of STRING is the null character, ASCII code 0.
  750.           (string-to-char "ABC")
  751.                => 65
  752.           (string-to-char "xyz")
  753.                => 120
  754.           (string-to-char "")
  755.                => 0
  756.           (string-to-char "\000")
  757.                => 0
  758.      This function may be eliminated in the future if it does not seem
  759.      useful enough to retain.
  760.  - Function: number-to-string NUMBER
  761.  - Function: int-to-string NUMBER
  762.      This function returns a string consisting of the printed
  763.      representation of NUMBER, which may be an integer or a floating
  764.      point number.  The value starts with a sign if the argument is
  765.      negative.
  766.           (int-to-string 256)
  767.                => "256"
  768.           (int-to-string -23)
  769.                => "-23"
  770.           (int-to-string -23.5)
  771.                => "-23.5"
  772.      See also the function `format' in *Note Formatting Strings::.
  773.  - Function: string-to-number STRING
  774.  - Function: string-to-int STRING
  775.      This function returns the integer value of the characters in
  776.      STRING, read as a number in base ten.  It skips spaces at the
  777.      beginning of STRING, then reads as much of STRING as it can
  778.      interpret as a number.  (On some systems it ignores other
  779.      whitespace at the beginning, not just spaces.)  If the first
  780.      character after the ignored whitespace is not a digit or a minus
  781.      sign, this function returns 0.
  782.           (string-to-number "256")
  783.                => 256
  784.           (string-to-number "25 is a perfect square.")
  785.                => 25
  786.           (string-to-number "X256")
  787.                => 0
  788.           (string-to-number "-4.5")
  789.                => -4.5
  790. File: elisp,  Node: Formatting Strings,  Next: Character Case,  Prev: String Conversion,  Up: Strings and Characters
  791. Formatting Strings
  792. ==================
  793.    "Formatting" means constructing a string by substitution of computed
  794. values at various places in a constant string.  This string controls
  795. how the other values are printed as well as where they appear; it is
  796. called a "format string".
  797.    Formatting is often useful for computing messages to be displayed.
  798. In fact, the functions `message' and `error' provide the same
  799. formatting feature described here; they differ from `format' only in
  800. how they use the result of formatting.
  801.  - Function: format STRING &rest OBJECTS
  802.      This function returns a new string that is made by copying STRING
  803.      and then replacing any format specification in the copy with
  804.      encodings of the corresponding OBJECTS.  The arguments OBJECTS are
  805.      the computed values to be formatted.
  806.    A format specification is a sequence of characters beginning with a
  807. `%'.  Thus, if there is a `%d' in STRING, the `format' function
  808. replaces it with the printed representation of one of the values to be
  809. formatted (one of the arguments OBJECTS).  For example:
  810.      (format "The value of fill-column is %d." fill-column)
  811.           => "The value of fill-column is 72."
  812.    If STRING contains more than one format specification, the format
  813. specifications are matched with successive values from OBJECTS.  Thus,
  814. the first format specification in STRING is matched with the first such
  815. value, the second format specification is matched with the second such
  816. value, and so on.  Any extra format specifications (those for which
  817. there are no corresponding values) cause unpredictable behavior.  Any
  818. extra values to be formatted will be ignored.
  819.    Certain format specifications require values of particular types.
  820. However, no error is signaled if the value actually supplied fails to
  821. have the expected type.  Instead, the output is likely to be
  822. meaningless.
  823.    Here is a table of the characters that can follow `%' to make up a
  824. format specification:
  825.      Replace the specification with the printed representation of the
  826.      object, made without quoting.  Thus, strings are represented by
  827.      their contents alone, with no `"' characters, and symbols appear
  828.      without `\' characters.
  829.      If there is no corresponding object, the empty string is used.
  830.      Replace the specification with the printed representation of the
  831.      object, made with quoting.  Thus, strings are enclosed in `"'
  832.      characters, and `\' characters appear where necessary before
  833.      special characters.
  834.      If there is no corresponding object, the empty string is used.
  835.      Replace the specification with the base-eight representation of an
  836.      integer.
  837.      Replace the specification with the base-ten representation of an
  838.      integer.
  839.      Replace the specification with the base-sixteen representation of
  840.      an integer.
  841.      Replace the specification with the character which is the value
  842.      given.
  843.      Replace the specification with the exponential notation for a
  844.      floating point number.
  845.      Replace the specification with the decimal-point notation for a
  846.      floating point number.
  847.      Replace the specification with notation for a floating point
  848.      number, using either exponential notation or decimal-point
  849.      notation whichever is shorter.
  850.      A single `%' is placed in the string.  This format specification is
  851.      unusual in that it does not use a value.  For example, `(format "%%
  852.      %d" 30)' returns `"% 30"'.
  853.    Any other format character results in an `Invalid format operation'
  854. error.
  855.    Here are several examples:
  856.      (format "The name of this buffer is %s." (buffer-name))
  857.           => "The name of this buffer is strings.texi."
  858.      
  859.      (format "The buffer object prints as %s." (current-buffer))
  860.           => "The buffer object prints as #<buffer strings.texi>."
  861.      
  862.      (format "The octal value of 18 is %o,
  863.               and the hex value is %x." 18 18)
  864.           => "The octal value of 18 is 22,
  865.               and the hex value is 12."
  866.    All the specification characters allow an optional numeric prefix
  867. between the `%' and the character.  The optional numeric prefix defines
  868. the minimum width for the object.  If the printed representation of the
  869. object contains fewer characters than this, then it is padded.  The
  870. padding is on the left if the prefix is positive (or starts with zero)
  871. and on the right if the prefix is negative.  The padding character is
  872. normally a space, but if the numeric prefix starts with a zero, zeros
  873. are used for padding.
  874.      (format "%06d will be padded on the left with zeros" 123)
  875.           => "000123 will be padded on the left with zeros"
  876.      
  877.      (format "%-6d will be padded on the right" 123)
  878.           => "123    will be padded on the right"
  879.    `format' never truncates an object's printed representation, no
  880. matter what width you specify.  Thus, you can use a numeric prefix to
  881. specify a minimum spacing between columns with no risk of losing
  882. information.
  883.    In the following three examples, `%7s' specifies a minimum width of
  884. 7.  In the first case, the string inserted in place of `%7s' has only 3
  885. letters, so 4 blank spaces are inserted for padding.  In the second
  886. case, the string `"specification"' is 13 letters wide but is not
  887. truncated.  In the third case, the padding is on the right.
  888.      (format "The word `%7s' actually has %d letters in it." "foo"
  889.              (length "foo"))
  890.           => "The word `    foo' actually has 3 letters in it."
  891.      (format "The word `%7s' actually has %d letters in it."
  892.              "specification"
  893.              (length "specification"))
  894.           => "The word `specification' actually has 13 letters in it."
  895.      (format "The word `%-7s' actually has %d letters in it." "foo"
  896.              (length "foo"))
  897.           => "The word `foo    ' actually has 3 letters in it."
  898. File: elisp,  Node: Character Case,  Next: Case Table,  Prev: Formatting Strings,  Up: Strings and Characters
  899. Character Case
  900. ==============
  901.    The character case functions change the case of single characters or
  902. of the contents of strings.  The functions convert only alphabetic
  903. characters (the letters `A' through `Z' and `a' through `z'); other
  904. characters are not altered.  The functions do not modify the strings
  905. that are passed to them as arguments.
  906.    The examples below use the characters `X' and `x' which have ASCII
  907. codes 88 and 120 respectively.
  908.  - Function: downcase STRING-OR-CHAR
  909.      This function converts a character or a string to lower case.
  910.      When the argument to `downcase' is a string, the function creates
  911.      and returns a new string in which each letter in the argument that
  912.      is upper case is converted to lower case.  When the argument to
  913.      `downcase' is a character, `downcase' returns the corresponding
  914.      lower case character.  This value is an integer.  If the original
  915.      character is lower case, or is not a letter, then the value equals
  916.      the original character.
  917.           (downcase "The cat in the hat")
  918.                => "the cat in the hat"
  919.           
  920.           (downcase ?X)
  921.                => 120
  922.  - Function: upcase STRING-OR-CHAR
  923.      This function converts a character or a string to upper case.
  924.      When the argument to `upcase' is a string, the function creates
  925.      and returns a new string in which each letter in the argument that
  926.      is lower case is converted to upper case.
  927.      When the argument to `upcase' is a character, `upcase' returns the
  928.      corresponding upper case character.  This value is an integer.  If
  929.      the original character is upper case, or is not a letter, then the
  930.      value equals the original character.
  931.           (upcase "The cat in the hat")
  932.                => "THE CAT IN THE HAT"
  933.           
  934.           (upcase ?x)
  935.                => 88
  936.  - Function: capitalize STRING-OR-CHAR
  937.      This function capitalizes strings or characters.  If
  938.      STRING-OR-CHAR is a string, the function creates and returns a new
  939.      string, whose contents are a copy of STRING-OR-CHAR in which each
  940.      word has been capitalized.  This means that the first character of
  941.      each word is converted to upper case, and the rest are converted
  942.      to lower case.
  943.      The definition of a word is any sequence of consecutive characters
  944.      that are assigned to the word constituent category in the current
  945.      syntax table (*Note Syntax Class Table::).
  946.      When the argument to `capitalize' is a character, `capitalize' has
  947.      the same result as `upcase'.
  948.           (capitalize "The cat in the hat")
  949.                => "The Cat In The Hat"
  950.           
  951.           (capitalize "THE 77TH-HATTED CAT")
  952.                => "The 77th-Hatted Cat"
  953.           
  954.           (capitalize ?x)
  955.                => 88
  956.